#include "TagPrintControl.h"
//#include "libusb/lusb0_usb.h"

#include <Windows.h>

#include <QPrinterInfo>
#include <QPrinter>
#include <QTextDocument>


#define PRINT_DEVICE_NAME "ZDesigner ZD888-203dpi ZPL"
//"ZDesigner ZT210-200dpi ZPL"

TagPrintControl::TagPrintControl(QObject* parent)
	: QObject(parent)
{
    //
}

TagPrintControl::~TagPrintControl()
{
		
}

void TagPrintControl::Start()
{
        //
}

void TagPrintControl::Stop()
{
		
}

BOOL RawDataToPrinter(LPSTR szPrinterName, LPBYTE lpData, DWORD dwCount)
{
    HANDLE     hPrinter;
    DOC_INFO_1 DocInfo;
    DWORD      dwJob;
    DWORD      dwBytesWritten;


    qDebug() << szPrinterName;

    // Need a handle to the printer.
    /*if (!OpenPrinter((CHAR*)PRINT_DEVICE_NAME, &hPrinter, NULL))
    {
        qDebug() << "Error opening printer!";
        return FALSE;
    }*/

    if (!OpenPrinterA((CHAR*)PRINT_DEVICE_NAME, &hPrinter, NULL))  // 显式调用 A 版本
    {
        qDebug() << "Error opening printer!" << GetLastError();
        return FALSE;
    }
            
    // Fill in the structure with info about this "document."

    const wchar_t* name = L"My Document";
    const wchar_t* raw = L"RAW";
    DocInfo.pDocName = (LPWSTR)name;
    DocInfo.pOutputFile = NULL;
    DocInfo.pDatatype = (LPWSTR)raw;
    // Inform the spooler the document is beginning.
    if ((dwJob = StartDocPrinter(hPrinter, 1, (LPBYTE)(LPSTR)&DocInfo)) == 0)
    {
        qDebug() << " Inform the spooler the document is beginning" << GetLastError();
        ClosePrinter(hPrinter);
        return FALSE;
    }
    // Start a page.
    if (!StartPagePrinter(hPrinter))
    {
        qDebug() << " Start a page. " << GetLastError();
        EndDocPrinter(hPrinter);
        ClosePrinter(hPrinter);
        return FALSE;
    }
    // Send the data to the printer.
    if (!WritePrinter(hPrinter, lpData, dwCount, &dwBytesWritten))
    {
        qDebug() << " Send the data to the printer." << GetLastError();
        EndPagePrinter(hPrinter);
        EndDocPrinter(hPrinter);
        ClosePrinter(hPrinter);
        return FALSE;
    }
    // End the page.
    if (!EndPagePrinter(hPrinter))
    {
        qDebug() << " End the page " << GetLastError();
        EndDocPrinter(hPrinter);
        ClosePrinter(hPrinter);
        return FALSE;
    }
    // Inform the spooler that the document is ending.
    if (!EndDocPrinter(hPrinter))
    {
        qDebug() << " Inform the spooler that the document is ending" << GetLastError();
        ClosePrinter(hPrinter);
        return FALSE;
    }
    // Tidy up the printer handle.
    ClosePrinter(hPrinter);
    // Check to see if correct number of bytes were written.
    if (dwBytesWritten != dwCount)
    {
        qDebug() << " Check to see if correct number of bytes were written. " << GetLastError();
        return FALSE;
    }
          
    return TRUE;
}

void TagPrintControl::SendContentPrint(const QString& printDevice,const QString& printContent)
{
	QByteArray content = printContent.toUtf8();
    //ZDesigner ZT210-200dpi ZPL
    qDebug() << printDevice;

    QByteArray barry = printDevice.toLatin1();
    const char* name = barry.data();
    if (RawDataToPrinter((char*)name, (LPBYTE)content.data(), content.size()))
    {
        qDebug() << "print success";
    }
    else
    {
        qDebug() << "打印失败:";
    }
}

QStringList TagPrintControl::GetTagPrintInstall()
{
    QStringList printerList = QPrinterInfo::availablePrinterNames();
    return printerList;
}
